package com.example.jl.bethinner.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.jl.bethinner.R;
import com.example.jl.bethinner.base.BaseActivity;


public class WebViewActivity extends BaseActivity {
    private WebView webView;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        webView = (WebView) findViewById(R.id.webview);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setBuiltInZoomControls(false);

        Intent i = getIntent();

        url = "https://mp.weixin.qq.com/s?__biz=MzAxNTAyOTgzNw==&mid=214729045&idx=1&sn=80ceeacb77705c52a3941538de8e779f&scene=1&srcid=XwoBrZIxLq0Y0Jhu1w97&key=dffc561732c226514768f0bda06ca1c4f555ce13e43ad1fd179fccb0848a04019eacfcc9d1470cf5db514bc7bd7ccde3&ascene=0&uin=MzAyNzMwMzU1&devicetype=iMac+Macmini7%2C1+OSX+OSX+10.10.4+build(14E46)&version=11020113&pass_ticket=DK5193LoHI9B62b7OPHBdrx06iNNSqtID%2Fitl96F%2Fl4GhXj8RLBKHpV62UVSxZmb";
//        url = "http://fir.im/mhpv";
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.setWebViewClient(new MyWebViewClient());
        webView.getSettings().setBuiltInZoomControls(false);
        webView.addJavascriptInterface(new InJavaScriptLocalObj(), "local_obj");
        webView.loadUrl(url);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_web_view, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void WebViewBack() {
        if (webView.canGoBack())
            webView.goBack();
        else finish();
    }
    class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith("tel:")) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
            } else if (url.startsWith("http:") || url.startsWith("https:")) {
                view.loadUrl(url);
            }
            return true;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
        }
        public void onPageFinished(WebView view, String url) {
            Log.d("WebView","onPageFinished ");
            view.loadUrl("javascript:window.local_obj.showSource('<body>'+document.getElementsByTagName('html')[0].innerHTML+'</body>');");
//            view.documentHasImages();
            super.onPageFinished(view, url);
        }
    }
    final class InJavaScriptLocalObj {
        @JavascriptInterface
        public void showSource(String html) {
            Log.d("HTMLString", html);
        }
    }
}
